/**
 *
 *@param {*base64字符串} dataurl
 *@return {Blob文件类型}
 */
export function dataURLtoBlob(dataurl) {
  let arr = dataurl.split(',')
  let mime = arr[0].match(/:(.*?);/)[1]
  let bstr = atob(arr[1])
  let n = bstr.length
  let u8arr = new Uint8Array(n)
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n)
  }
  return new Blob([u8arr], { type: mime })
}

/**
 * 采用递归算法，在移动端也测试过，10M也基本没问题，现在的手机随便一张照片
 * 就上3M了，这可以减少不少服务器的负担
 *
 *@param {*img标签的加载事件参数} img
 *@return {*处理最终结果返回图片base64编码} base64data
 *
 */
export function scaleimg(img) {
  let height = img.currentTarget.height
  let width = img.currentTarget.width
  let base64data = ''
  let can = document.createElement("canvas")
  can.height = height
  can.width = width
  let casimg = can.getContext('2d')
  casimg.clearRect(0, 0, width, height)
  casimg.drawImage(img.currentTarget, 0, 0, width, height)
  debugger
  casimg.fillStyle = "#FF0000"
  casimg.strokeStyle = "#FF0000"
  base64data = can.toDataURL('image/png')
  let size = Math.round(dataURLtoBlob(base64data).size / 1024)
  let maxsize = 800
  if (size > maxsize) {
    if (size < 1300) {
      img.currentTarget.height = Math.round(height * (3 / 4))
      img.currentTarget.width = Math.round(width * (3 / 4))
      return scaleimg(img)
    } else if (size < 1800) {
      img.currentTarget.height = Math.round(height * (2 / 3))
      img.currentTarget.width = Math.round(width * (2 / 3))
      return scaleimg(img)
    } else {
      img.currentTarget.height = Math.round(height / 2)
      img.currentTarget.width = Math.round(width / 2)
      return scaleimg(img)
    }
  } else {
    return base64data
  }
}
